home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / SML⁄NJ 93+ / Documentation / examples / awk / lex.sml < prev    next >
Encoding:
Text File  |  1995-12-30  |  1.0 KB  |  56 lines  |  [TEXT/R*ch]

  1. (* lex.sml *)
  2.  
  3. signature LEX =
  4. sig
  5.   val words: string -> string list
  6. end
  7.  
  8. structure Lex1 : LEX =
  9. struct
  10.  
  11.   fun separator " " = true
  12.     | separator "\t" = true
  13.     | separator "\n" = true
  14.     | separator _ = false
  15.  
  16.   fun words s =
  17.       let fun getword(w,[])  = [implode(rev w)]
  18.         | getword(w,c::rest) = 
  19.         if separator(c)
  20.         then implode(rev w) :: skip rest
  21.         else getword(c::w,rest)
  22.       and skip [] = []
  23.         | skip(c::rest) =
  24.         if separator c
  25.         then skip rest
  26.         else getword([c],rest)
  27.        in skip(explode s)
  28.       end
  29.  
  30. end (* Lex1 *)
  31.  
  32. structure Lex2 : LEX =
  33. struct
  34.  
  35.   fun separator " " = true
  36.     | separator "\t" = true
  37.     | separator "\n" = true
  38.     | separator _ = false
  39.  
  40.   fun words(s: string) =
  41.       let val len  = size s
  42.       fun skip n = 
  43.           let fun getword m =
  44.           if m>=len orelse separator(substring(s,m,1))
  45.           then substring(s,n,(m-n))::skip(m+1)
  46.           else getword(m+1)
  47.            in if n>=len
  48.           then []
  49.           else if separator(substring(s,n,1))
  50.           then skip(n+1)
  51.           else getword(n+1)
  52.           end
  53.        in skip 0
  54.       end
  55.  
  56. end (* Lex2 *)